package com.example.fw;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.example.obj.GroupObject;
import com.example.obj.GroupSet;

public class GroupHelper extends HelperBase {
	
	private static GroupSet groupCache;

	public GroupHelper(ApplicationManager manager) {
		super(manager);		
	}

	public GroupSet getGroupsCouche() {
		log.info("Get Group Cache");
		if (groupCache == null){
			buildGroupCache();			
		}
		return groupCache;
	}
	
	private void buildGroupCache(){		
		groupCache = manager.getHibernateHelper().getGroups();
	}
	
	public GroupSet getGroupsUI(){
		log.info("Get Groups from UI");
		manager.getNavigationHelper().openGroupListPage();
		WebElement form = findElements(By.tagName(manager.getProperty("groupsForm", "form"))).get(1);
		List<WebElement> checkboxes = findElements(form, By.name(manager.getProperty("groupsCheckbox", "selected[]")));
		GroupSet groups = new GroupSet();
		for (WebElement checkbox : checkboxes) {
			// i get value in outer (): [^\(]*\((.*)\)
			String name = "";
			Pattern p = Pattern.compile("[^\\(]*\\((.*)\\)");
			Matcher m = p.matcher(checkbox.getAttribute(manager.getProperty("groupsName", "title")));			
			if (m.matches()){ name = m.group(1); }
			GroupObject group = new GroupObject()
				.setId(checkbox.getAttribute(manager.getProperty("groupsId", "value")))
				.setName(name);
			groups.add(group);
		}		
		return groups;
	}
	
	public void createGroup(GroupObject groupObject) {	
		log.info("Add group " + groupObject + " to the list");
		manager.getNavigationHelper().openGroupListPage();
		initGroupCreation();		
		fillGroupData(groupObject);
		submitGroupCreation();
		returnToGroupListPage();
		buildGroupCache();
	}
	
	public void deleteGroup(String id) {
		log.info("Delete group " + id + " from the list");
		manager.getNavigationHelper().openGroupListPage();
		checkElementByID(id);
		submitGroupDeletion();
		returnToGroupListPage();
		buildGroupCache();
	}
	
	public void modifyGroup(String id, GroupObject groupObject) {	
		log.info("Modify group " + id + " to " + groupObject);
		manager.getNavigationHelper().openGroupListPage();		
		initGroupModification(id);
		fillGroupData(groupObject);
		submitGroupModification();
		returnToGroupListPage();
		buildGroupCache();
	}

	private void initGroupCreation() {
		click(By.name(manager.getProperty("groupsAddNewButton", "new")));
	}
	
	private void initGroupModification(String id) {
		checkElementByID(id);
		click(By.name(manager.getProperty("groupsEditButton", "edit")));
	}

	private void submitGroupCreation() {
		click(By.name(manager.getProperty("groupsSubmitNewButton", "submit")));
	}
	
	private void submitGroupDeletion() {
		click(By.name(manager.getProperty("groupsSubmitDelButton", "delete")));
	}
	
	private void submitGroupModification() {
		click(By.name(manager.getProperty("groupsSubmitUpdButton", "update")));
	}	
	private void fillGroupData(GroupObject groupObject) {
		type(By.name("group_name"), groupObject.name);
		type(By.name("group_header"), groupObject.header);
		type(By.name("group_footer"), groupObject.footer);		
	}
	
	private void returnToGroupListPage() {
		click(By.linkText(manager.getProperty("groupsListReturnLink", "group page")));
	}

	public GroupObject getGroupUI(String id) {
		manager.getNavigationHelper().openGroupListPage();		
		initGroupModification(id);	
		return getGroupData();
	}

	private GroupObject getGroupData() {
		GroupObject group = new GroupObject()
			.setName(findElement(By.name("group_name")).getAttribute("value"))
			.setHeader(findElement(By.name("group_header")).getAttribute("value"))
			.setFooter(findElement(By.name("group_footer")).getAttribute("value"));
		return group;
		
	}

}
